package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.service.MemeberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.jws.Oneway;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员服务业务逻辑处理层
 * @author wangxin
 * @version 1.0
 */
@Service(interfaceClass = MemeberService.class)
@Transactional
public class MemeberServiceImpl implements MemeberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 根据年月获取累计会员数量
     * @param listMonth
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> listMonth) {
        //定义list集合存放会员数量
        List<Integer> memberCount = new ArrayList<>();
        if(listMonth != null && listMonth.size()>0){
            for (String yearMonth : listMonth) {
                //2020-02 select count(*) from t_member where regTime <= '2020-02-31'
                yearMonth = yearMonth+"-31";
                Integer count = memberDao.findMemberCountBeforeDate(yearMonth);
                memberCount.add(count);
            }
        }
        return memberCount;
    }
    /**
     * 获取男性会员数据
     */
    @Override
    public Map<String, Object> getMemberSex() {
        return memberDao.getMemberSex();
    }
    /**
     * 获取女性会员数据
     */
    @Override
    public Map<String, Object> getMemberSex2() {
        return memberDao.getMemberSex2();
    }
    /**
     * 获取会员年龄占比数据
     */
    @Override
    public List<Map<String, Object>> getMemberageGroup() {
        int age1 = 0;//0岁-18岁
        int age2 = 0;//18岁-30岁
        int age3 = 0;//30岁-45岁
        int age4 = 0;//45岁-65岁
        int age5 = 0;//65岁以上
        //通过身份证查询到会员年龄,用List接收
        List<String> allMage = memberDao.getMemberageGroup();
        //遍历年龄数组,获取各年龄人数
        for (String age : allMage) {
            if (age != null) {
                int ages = Integer.parseInt(age);
                if (ages > 0 && ages <= 18) {
                    age1++;
                }
                if (ages > 18 && ages <= 30) {
                    age2++;
                }
                if (ages > 30 && ages <= 45) {
                    age3++;
                }
                if (ages > 45 && ages <= 65) {
                    age4++;
                }
                if (ages > 65) {
                    age5++;
                }
            }
        }
        //对年龄进行分类
        Map<String,Object> Agemap1 = new HashMap<>();
        Map<String,Object> Agemap2 = new HashMap<>();
        Map<String,Object> Agemap3 = new HashMap<>();
        Map<String,Object> Agemap4 = new HashMap<>();
        Map<String,Object> Agemap5 = new HashMap<>();
        //为了让没有数据的分类不显示在页面,这里需要设置分类人数不为0
        if (age1 !=0) {
            Agemap1.put("name", "0岁-18岁");
            Agemap1.put("value", age1);
        }
        if (age2 !=0) {
            Agemap2.put("name", "18岁-30岁");
            Agemap2.put("value", age2);
        }
        if (age3 !=0) {
            Agemap3.put("name", "30岁-45岁");
            Agemap3.put("value", age3);
        }
        if (age4 !=0) {
            Agemap4.put("name", "45岁-65岁");
            Agemap4.put("value", age4);
        }
        if (age5 !=0) {
            Agemap5.put("name", "65岁以上");
            Agemap5.put("value", age5);
        }
        //用List接收分好类的Map集合
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(Agemap1);
        list.add(Agemap2);
        list.add(Agemap3);
        list.add(Agemap4);
        list.add(Agemap5);
        return list;
    }


    //通过日期查找会员数量
    @Override
    public Integer findMemberCountByDate(String date) {
        Integer count = memberDao.findMemberCountBeforeDate(date);
        return count;
    }

}
